﻿//Copyright (C) <2013>  <jonathan cleeve norton> All Rights Reserved 
//Contact jon.norton@fin-plus.co.uk website <http://www.fin-plus.co.uk/>
using System;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Reflection;
using FinPlusAssembler;
using FinPlusInterfaces;
using FinPlusCompView;

namespace FinPlusDesktop
{
    public partial class MainWindow : Window, IFinPlusComp
    {
        [State(Test = true)]
        public string TestString { get { return string.Empty; } }
        [State(Test = true)]
        public bool IsValid { get; set; }
        [State(Save = true)]
        public new double Left { get { return base.Left; } set { base.Left = value; } }
        [State(Save = true)]
        public new double Top { get { return base.Top; } set { base.Top = value; } }
        [State(Save = true)]
        public new double Height { get { return base.Height; } set { base.Height = value; } }
        [State(Save = true)]
        public new double Width { get { return base.Width; } set { base.Width = value; } }
        [State(Test = true)]
        public int ChildrenCount { get { return grid.Children.Count; } }
        [State(Save = true)]
        public double TabPosition { get { return padding.Width; } set { padding.Width = value; } }
        [State(Save = true)]
        public double ScrollOffset { get { return scrollViewer.VerticalOffset; } set { scrollViewer.ScrollToVerticalOffset(value); } }
        
        public string Id { get; set; }
        public string LinkedId { get; set; }
        public string Config { get; set; }
        public Env Env { get; set; }
        public IPods Adapter { get { return null; } }

        private IPod _state;
        private readonly FinPlusView _view;
        private readonly string _templatePath;
        private const int _frameRate = 15;
        private Env _env;

        //construct
        public MainWindow()
        {
            InitializeComponent();
            Cursor = ((TextBlock)this.Resources["CPoint"]).Cursor;
            Id = "FinPlusDesktop";
            _templatePath = Paths.Values["FolderSubViews"] + Id;

            _env = Assembly.GetExecutingAssembly().ConfigPath().ConfigEnv();
            grid.Children.Add(_view = new FinPlusView(_templatePath, Id, _env));
            status.Children.Add(new FinPlusStatusBar());

            LoadState();

            Timeline.DesiredFrameRateProperty.OverrideMetadata(typeof(Timeline), new FrameworkPropertyMetadata { DefaultValue = _frameRate });
        }

        //common control interface 
        public void Dock(object component, string dock) {}

        public void CmdExecute(IPod command) {}

        public void Dispose()
        {
            if (_view != null)
                _view.Dispose();
        }

        //public
        public void SaveState()
        {
            try
            {
                var xmlDoc = _state.ExtractState(Environment.UserName).ToXmlDoc();
                xmlDoc.Save(string.Format("{0}.{1}", _templatePath, FileType.state.ToString()));
            }
            catch (Exception e)
            {
                Level.Warning.Log(e.Message, e);
            }
        }

        //private
        private void LoadState()
        {
            try
            {
                _state = IOC.New<IPod>("FinPlusDesktop", "finPlusDesktop", Environment.UserName);
                _state.Object = finPlusDesktop;
                _state.InjectState(_templatePath);
            }
            catch (Exception e)
            {
                Level.Warning.Log(e.Message, e);
            }
        }

        //events
        private void Window_Activated(object s, EventArgs a) {  }
        private void Window_Deactived(object s, EventArgs a) {this.Topmost = true; this.Activate();}
        private void Window_OnMaximize(object s, RoutedEventArgs a)
        {
            if (WindowState == WindowState.Maximized)
                WindowState = WindowState.Normal;
            else if (WindowState == WindowState.Normal)
                WindowState = WindowState.Maximized;
        }
        private void Window_OnMinimize(object s, RoutedEventArgs a) {  WindowState = WindowState.Minimized; }
        private void Window_OnClose(object s, RoutedEventArgs a) { _view.Dispose(); Close(); }
        private void Window_OnSave(object s, RoutedEventArgs a) { SaveState(); }
        private void Window_Loaded(object s, RoutedEventArgs a) { this.MaxWidth = this.MinWidth = this.ActualWidth;}
        private void Title_MouseDown(object s, MouseButtonEventArgs a){ try{ this.DragMove();}catch{}}
        private void Scroll_Up(object s, MouseButtonEventArgs a) { scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset - 20); }
        private void Scroll_Down(object s, MouseButtonEventArgs a) { scrollViewer.ScrollToVerticalOffset(scrollViewer.VerticalOffset + 20); }

    }
}
